//
//  AppDelegate.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/1.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UIAlertViewDelegate {
    
    var window: UIWindow?
    
    let tool = WXTool()
    
//    lazy var wx_tool = Weixin()
    
    let qqTool = QQTool()
    
    lazy var tencentOAuth = TencentOAuth()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//        self.setWX()
        
        // 创建window
//        window = UIWindow(frame: UIScreen.main.bounds)
//        
//        ///启动界面加导航栏
//        let nav : UINavigationController = UINavigationController(rootViewController: UserVC())
//        
//        
//        window?.rootViewController = nav
//        window?.makeKeyAndVisible()
       //启动主界面
        setMainBar()
        
        //检测网络状态
//        checkNetStatus()
        return true
    }
}

// MARK:- 若已退出，重新生命，重新请求加载
extension AppDelegate {
    func applicationWillResignActive(_ application: UIApplication) {
        
        
        //取出本地用户名。密码
        guard let pass = localSave.object(forKey: localPass) as? String else {
            return
        }
        
        guard let uname = localSave.object(forKey: localName) as? String else {
            return
        }

        LoginModel.shared.loginWithLocalInfo(tfNum: uname, tfPass: pass) { (result) in
            
        }
        
    }
}

extension AppDelegate {
    func checkNetStatus() -> Void {
        CheckTool.shared.check { (netStatus) in
            print("工具类检测",netStatus)
            
            if netStatus == "No Connection" {
                let alertController = UIAlertController.init(title: "提示", message: "网络连接失败", preferredStyle: .alert)
                
                alertController.addAction(UIAlertAction.init(title: "确定", style: .destructive, handler: nil))
                UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
            }
        }

    }
}

// MARK:- 登录
extension AppDelegate {
    func loginSEL() -> Void {
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        ///启动界面加导航栏
        let nav : UINavigationController = UINavigationController(rootViewController: UserVC())
        
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
    }
}

// MARK:- 设置主控制器
extension AppDelegate {
    func setMainBar() -> Void {
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.makeKeyAndVisible()
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")

        let customtabbar = MainViewController()
        window?.rootViewController = customtabbar
        
        setQQ()
        setWX()

        // 设置全局颜色
        UITabBar.appearance().tintColor = UIColor.orange
        UINavigationBar.appearance().tintColor = UIColor.orange
    }
}

// MARK:- 第三方介入
extension AppDelegate {
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let wxUrl = String(format: "%@", arguments: [WXPatient_App_ID])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
                        return WXApi.handleOpen(url, delegate: tool)
//            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
            //微信支付
        }
        else
            if url.scheme == WXPatient_App_ID {
            return WXApi.handleOpen(url, delegate: tool)
        }
        return true;
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let wxUrl = String(format: "%@", arguments: [WXPatient_App_ID])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
                        return WXApi.handleOpen(url, delegate: tool)
//            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
    
    // MARK:- 微信分享
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        
        let wxUrl = String(format: "%@", arguments: [WXPatient_App_ID])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
                        return WXApi.handleOpen(url, delegate: tool)
//            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
}

// MARK:- 微信
extension AppDelegate {
    
    func setWX() -> Void {
        WXApi.registerApp(WXPatient_App_ID)
//        wx_tool.requestForUserInfoBlock = {() -> Void in
//            Weixin.shareDataSource().wechatLoginByRequestForUserInfo()
//        }
//        tool.requestForUserInfoBlock = {() -> Void in
//            WXTool.shared.wechatLoginByRequestForUserInfo()
//        }
        
    }
}

// MARK:- QQ
extension AppDelegate {
    func setQQ() -> Void {
        tencentOAuth = TencentOAuth(appId: QQAppID, andDelegate: qqTool)
    }
}



 

